<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.middleware.common &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../index.html" />
    <link rel="up" title="django" href="../../django.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-middleware-common">
            
  <h1>Source code for django.middleware.common</h1><div class="highlight"><pre>
<span class="kn">import</span> <span class="nn">hashlib</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">warnings</span>

<span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.core.mail</span> <span class="kn">import</span> <span class="n">mail_managers</span>
<span class="kn">from</span> <span class="nn">django.core</span> <span class="kn">import</span> <span class="n">urlresolvers</span>
<span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">http</span>
<span class="kn">from</span> <span class="nn">django.utils.deprecation</span> <span class="kn">import</span> <span class="n">RemovedInDjango18Warning</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="kn">import</span> <span class="n">force_text</span>
<span class="kn">from</span> <span class="nn">django.utils.http</span> <span class="kn">import</span> <span class="n">urlquote</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">six</span>


<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s">&#39;django.request&#39;</span><span class="p">)</span>


<div class="viewcode-block" id="CommonMiddleware"><a class="viewcode-back" href="../../../ref/middleware.html#django.middleware.common.CommonMiddleware">[docs]</a><span class="k">class</span> <span class="nc">CommonMiddleware</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    &quot;Common&quot; middleware for taking care of some basic operations:</span>

<span class="sd">        - Forbids access to User-Agents in settings.DISALLOWED_USER_AGENTS</span>

<span class="sd">        - URL rewriting: Based on the APPEND_SLASH and PREPEND_WWW settings,</span>
<span class="sd">          this middleware appends missing slashes and/or prepends missing</span>
<span class="sd">          &quot;www.&quot;s.</span>

<span class="sd">            - If APPEND_SLASH is set and the initial URL doesn&#39;t end with a</span>
<span class="sd">              slash, and it is not found in urlpatterns, a new URL is formed by</span>
<span class="sd">              appending a slash at the end. If this new URL is found in</span>
<span class="sd">              urlpatterns, then an HTTP-redirect is returned to this new URL;</span>
<span class="sd">              otherwise the initial URL is processed as usual.</span>

<span class="sd">        - ETags: If the USE_ETAGS setting is set, ETags will be calculated from</span>
<span class="sd">          the entire page content and Not Modified responses will be returned</span>
<span class="sd">          appropriately.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">process_request</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Check for denied User-Agents and rewrite the URL based on</span>
<span class="sd">        settings.APPEND_SLASH and settings.PREPEND_WWW</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c"># Check for denied User-Agents</span>
        <span class="k">if</span> <span class="s">&#39;HTTP_USER_AGENT&#39;</span> <span class="ow">in</span> <span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">user_agent_regex</span> <span class="ow">in</span> <span class="n">settings</span><span class="o">.</span><span class="n">DISALLOWED_USER_AGENTS</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">user_agent_regex</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="p">[</span><span class="s">&#39;HTTP_USER_AGENT&#39;</span><span class="p">]):</span>
                    <span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&#39;Forbidden (User agent): </span><span class="si">%s</span><span class="s">&#39;</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">path</span><span class="p">,</span>
                        <span class="n">extra</span><span class="o">=</span><span class="p">{</span>
                            <span class="s">&#39;status_code&#39;</span><span class="p">:</span> <span class="mi">403</span><span class="p">,</span>
                            <span class="s">&#39;request&#39;</span><span class="p">:</span> <span class="n">request</span>
                        <span class="p">}</span>
                    <span class="p">)</span>
                    <span class="k">return</span> <span class="n">http</span><span class="o">.</span><span class="n">HttpResponseForbidden</span><span class="p">(</span><span class="s">&#39;&lt;h1&gt;Forbidden&lt;/h1&gt;&#39;</span><span class="p">)</span>

        <span class="c"># Check for a redirect based on settings.APPEND_SLASH</span>
        <span class="c"># and settings.PREPEND_WWW</span>
        <span class="n">host</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">get_host</span><span class="p">()</span>
        <span class="n">old_url</span> <span class="o">=</span> <span class="p">[</span><span class="n">host</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">path</span><span class="p">]</span>
        <span class="n">new_url</span> <span class="o">=</span> <span class="n">old_url</span><span class="p">[:]</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">PREPEND_WWW</span> <span class="ow">and</span> <span class="n">old_url</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">and</span>
                <span class="ow">not</span> <span class="n">old_url</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;www.&#39;</span><span class="p">)):</span>
            <span class="n">new_url</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;www.&#39;</span> <span class="o">+</span> <span class="n">old_url</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="c"># Append a slash if APPEND_SLASH is set and the URL doesn&#39;t have a</span>
        <span class="c"># trailing slash and there is no pattern for the current path</span>
        <span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">APPEND_SLASH</span> <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="n">old_url</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">)):</span>
            <span class="n">urlconf</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s">&#39;urlconf&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
            <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="n">urlresolvers</span><span class="o">.</span><span class="n">is_valid_path</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">path_info</span><span class="p">,</span> <span class="n">urlconf</span><span class="p">)</span> <span class="ow">and</span>
                    <span class="n">urlresolvers</span><span class="o">.</span><span class="n">is_valid_path</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">/&quot;</span> <span class="o">%</span> <span class="n">request</span><span class="o">.</span><span class="n">path_info</span><span class="p">,</span> <span class="n">urlconf</span><span class="p">)):</span>
                <span class="n">new_url</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">new_url</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="s">&#39;/&#39;</span>
                <span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEBUG</span> <span class="ow">and</span> <span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">&#39;POST&#39;</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">((</span><span class="s">&quot;&quot;</span>
                    <span class="s">&quot;You called this URL via POST, but the URL doesn&#39;t end &quot;</span>
                    <span class="s">&quot;in a slash and you have APPEND_SLASH set. Django can&#39;t &quot;</span>
                    <span class="s">&quot;redirect to the slash URL while maintaining POST data. &quot;</span>
                    <span class="s">&quot;Change your form to point to </span><span class="si">%s%s</span><span class="s"> (note the trailing &quot;</span>
                    <span class="s">&quot;slash), or set APPEND_SLASH=False in your Django &quot;</span>
                    <span class="s">&quot;settings.&quot;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">new_url</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">new_url</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>

        <span class="k">if</span> <span class="n">new_url</span> <span class="o">==</span> <span class="n">old_url</span><span class="p">:</span>
            <span class="c"># No redirects required.</span>
            <span class="k">return</span>
        <span class="k">if</span> <span class="n">new_url</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
            <span class="n">newurl</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">://</span><span class="si">%s%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="n">request</span><span class="o">.</span><span class="n">scheme</span><span class="p">,</span>
                <span class="n">new_url</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">urlquote</span><span class="p">(</span><span class="n">new_url</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">newurl</span> <span class="o">=</span> <span class="n">urlquote</span><span class="p">(</span><span class="n">new_url</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;QUERY_STRING&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY3</span><span class="p">:</span>
                <span class="n">newurl</span> <span class="o">+=</span> <span class="s">&#39;?&#39;</span> <span class="o">+</span> <span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="p">[</span><span class="s">&#39;QUERY_STRING&#39;</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c"># `query_string` is a bytestring. Appending it to the unicode</span>
                <span class="c"># string `newurl` will fail if it isn&#39;t ASCII-only. This isn&#39;t</span>
                <span class="c"># allowed; only broken software generates such query strings.</span>
                <span class="c"># Better drop the invalid query string than crash (#15152).</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">newurl</span> <span class="o">+=</span> <span class="s">&#39;?&#39;</span> <span class="o">+</span> <span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="p">[</span><span class="s">&#39;QUERY_STRING&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">decode</span><span class="p">()</span>
                <span class="k">except</span> <span class="ne">UnicodeDecodeError</span><span class="p">:</span>
                    <span class="k">pass</span>
        <span class="k">return</span> <span class="n">http</span><span class="o">.</span><span class="n">HttpResponsePermanentRedirect</span><span class="p">(</span><span class="n">newurl</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">process_response</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculate the ETag, if needed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">SEND_BROKEN_LINK_EMAILS</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&quot;SEND_BROKEN_LINK_EMAILS is deprecated. &quot;</span>
                <span class="s">&quot;Use BrokenLinkEmailsMiddleware instead.&quot;</span><span class="p">,</span>
                <span class="n">RemovedInDjango18Warning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
            <span class="n">BrokenLinkEmailsMiddleware</span><span class="p">()</span><span class="o">.</span><span class="n">process_response</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">USE_ETAGS</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">response</span><span class="o">.</span><span class="n">has_header</span><span class="p">(</span><span class="s">&#39;ETag&#39;</span><span class="p">):</span>
                <span class="n">etag</span> <span class="o">=</span> <span class="n">response</span><span class="p">[</span><span class="s">&#39;ETag&#39;</span><span class="p">]</span>
            <span class="k">elif</span> <span class="n">response</span><span class="o">.</span><span class="n">streaming</span><span class="p">:</span>
                <span class="n">etag</span> <span class="o">=</span> <span class="bp">None</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">etag</span> <span class="o">=</span> <span class="s">&#39;&quot;</span><span class="si">%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">md5</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">content</span><span class="p">)</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">etag</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                <span class="k">if</span> <span class="p">(</span><span class="mi">200</span> <span class="o">&lt;=</span> <span class="n">response</span><span class="o">.</span><span class="n">status_code</span> <span class="o">&lt;</span> <span class="mi">300</span>
                        <span class="ow">and</span> <span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;HTTP_IF_NONE_MATCH&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="n">etag</span><span class="p">):</span>
                    <span class="n">cookies</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">cookies</span>
                    <span class="n">response</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">HttpResponseNotModified</span><span class="p">()</span>
                    <span class="n">response</span><span class="o">.</span><span class="n">cookies</span> <span class="o">=</span> <span class="n">cookies</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">response</span><span class="p">[</span><span class="s">&#39;ETag&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">etag</span>

        <span class="k">return</span> <span class="n">response</span>

</div>
<div class="viewcode-block" id="BrokenLinkEmailsMiddleware"><a class="viewcode-back" href="../../../ref/middleware.html#django.middleware.common.BrokenLinkEmailsMiddleware">[docs]</a><span class="k">class</span> <span class="nc">BrokenLinkEmailsMiddleware</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">process_response</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Send broken link emails for relevant 404 NOT FOUND responses.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">response</span><span class="o">.</span><span class="n">status_code</span> <span class="o">==</span> <span class="mi">404</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">:</span>
            <span class="n">domain</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">get_host</span><span class="p">()</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">get_full_path</span><span class="p">()</span>
            <span class="n">referer</span> <span class="o">=</span> <span class="n">force_text</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;HTTP_REFERER&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">),</span> <span class="n">errors</span><span class="o">=</span><span class="s">&#39;replace&#39;</span><span class="p">)</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_ignorable_request</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">domain</span><span class="p">,</span> <span class="n">referer</span><span class="p">):</span>
                <span class="n">ua</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;HTTP_USER_AGENT&#39;</span><span class="p">,</span> <span class="s">&#39;&lt;none&gt;&#39;</span><span class="p">)</span>
                <span class="n">ip</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;REMOTE_ADDR&#39;</span><span class="p">,</span> <span class="s">&#39;&lt;none&gt;&#39;</span><span class="p">)</span>
                <span class="n">mail_managers</span><span class="p">(</span>
                    <span class="s">&quot;Broken </span><span class="si">%s</span><span class="s">link on </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span>
                        <span class="p">(</span><span class="s">&#39;INTERNAL &#39;</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_internal_request</span><span class="p">(</span><span class="n">domain</span><span class="p">,</span> <span class="n">referer</span><span class="p">)</span> <span class="k">else</span> <span class="s">&#39;&#39;</span><span class="p">),</span>
                        <span class="n">domain</span>
                    <span class="p">),</span>
                    <span class="s">&quot;Referrer: </span><span class="si">%s</span><span class="se">\n</span><span class="s">Requested URL: </span><span class="si">%s</span><span class="se">\n</span><span class="s">User agent: </span><span class="si">%s</span><span class="se">\n</span><span class="s">&quot;</span>
                    <span class="s">&quot;IP address: </span><span class="si">%s</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">referer</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">ua</span><span class="p">,</span> <span class="n">ip</span><span class="p">),</span>
                    <span class="n">fail_silently</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">response</span>

    <span class="k">def</span> <span class="nf">is_internal_request</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">domain</span><span class="p">,</span> <span class="n">referer</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns True if the referring URL is the same domain as the current request.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># Different subdomains are treated as different domains.</span>
        <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s">&quot;^https?://</span><span class="si">%s</span><span class="s">/&quot;</span> <span class="o">%</span> <span class="n">re</span><span class="o">.</span><span class="n">escape</span><span class="p">(</span><span class="n">domain</span><span class="p">),</span> <span class="n">referer</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">is_ignorable_request</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">uri</span><span class="p">,</span> <span class="n">domain</span><span class="p">,</span> <span class="n">referer</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns True if the given request *shouldn&#39;t* notify the site managers.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># &#39;?&#39; in referer is identified as search engine source</span>
        <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="n">referer</span> <span class="ow">or</span>
                <span class="p">(</span><span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_internal_request</span><span class="p">(</span><span class="n">domain</span><span class="p">,</span> <span class="n">referer</span><span class="p">)</span> <span class="ow">and</span> <span class="s">&#39;?&#39;</span> <span class="ow">in</span> <span class="n">referer</span><span class="p">)):</span>
            <span class="k">return</span> <span class="bp">True</span>
        <span class="k">return</span> <span class="nb">any</span><span class="p">(</span><span class="n">pattern</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span> <span class="k">for</span> <span class="n">pattern</span> <span class="ow">in</span> <span class="n">settings</span><span class="o">.</span><span class="n">IGNORABLE_404_URLS</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../index.html">Module code</a>
        
          <ul><li><a href="../../django.html">django</a>
        
        <ul><li>django.middleware.common</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>